package cn.bdqn.dao;

import cn.bdqn.entity.Student;

import java.sql.*;

public class StudentDao {
    //登录方法
    public Student login(String no, String pwd) {
        Student student = null;
        try {
            //注册
            Class.forName("com.mysql.jdbc.Driver");
            String connster = ("jdbc:mysql:///myschool?characterEncoding=utf8");
            //创建连接对象
            Connection conn = DriverManager.getConnection(connster, "root", "root");
            //创建表达式对象
            Statement stmt = conn.createStatement();

            String sql = "select * from student where studentNo='" + no + "'and loginPwd='" + pwd + "'";

            ResultSet rs = stmt.executeQuery(sql);
            student = null;
            if (rs.next()) {
                student = new Student();
                student.setStudentNo(rs.getString("StudentNo"));
                student.setLoginPwd(rs.getString("LoginPwd"));
                student.setStudentName(rs.getString("StudentName"));
                student.setSex(rs.getString("Sex"));
                student.setGradeId(rs.getInt("GradeId"));
                student.setPhone(rs.getString("Phone"));
                student.setAddress(rs.getString("Address"));
                student.setBornDate(rs.getDate("BornDate"));
                student.setEmail(rs.getString("Email"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return student;
    }

    //根据学生的名字找到对应的学号方法
    public String getNobyNane(String name) {
        Student student = null;
        try {
            //注册
            Class.forName("com.mysql.jdbc.Driver");
            String connster = ("jdbc:mysql:///myschool?characterEncoding=utf8");
            //创建连接对象
            Connection conn = DriverManager.getConnection(connster, "root", "root");
            //创建表达式对象
            Statement stmt = conn.createStatement();
            String sql = "select * from student where studentName='" + name + "'";
            ResultSet rs = stmt.executeQuery(sql);
            student = null;
            if (rs.next()) {
                student = new Student();
                student.setStudentNo(rs.getString("StudentNo"));
                student.setLoginPwd(rs.getString("LoginPwd"));
                student.setStudentName(rs.getString("StudentName"));
                student.setSex(rs.getString("Sex"));
                student.setGradeId(rs.getInt("GradeId"));
                student.setPhone(rs.getString("Phone"));
                student.setAddress(rs.getString("Address"));
                student.setBornDate(rs.getDate("BornDate"));
                student.setEmail(rs.getString("Email"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //返回学号(想要返回的值)
        return student.getStudentNo();
    }
}












